<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['sn'])){
		$params['sn like']='%'.trim($_RQ['sn']).'%';
		$return['params']['sn']=$_RQ['sn'];
	}
	if(!empty($_RQ['type'])){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=intval($_RQ['type']);
	}
	if(!empty($_RQ['kechengid'])){
		$params['kechengid']=intval($_RQ['kechengid']);
		$return['params']['kechengid']=intval($_RQ['kechengid']);
	}
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_peiedu_cert',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$xuesheng=pdo_get('jy_peiedu_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['xueshengid']));
		$item['title']=$xuesheng['title'];
		$item['mobile']=$xuesheng['mobile'];
		$item['sex']=$xuesheng['sex'];
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'证书管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delone'=>false,'delall'=>false,'list'=>array(
			array('name'=>'sn','title'=>'证书编号','type'=>'string'),
			array('name'=>'kechengid','title'=>'课程','type'=>'select','options'=>'kechengs'),
			array('name'=>'title','title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'codenumber','title'=>'身份证','type'=>'string'),
			array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
			array('name'=>'image','title'=>'生成证书','type'=>'image'),
			array('name'=>'image2','title'=>'最终证书','type'=>'image'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),		
		),'action'=>array(
			array('type'=>'href','action'=>'/cert/edit/','title'=>'更新证书'),
			array('type'=>'href','action'=>'/cert/cert/','title'=>'查看证书'),
			array('type'=>'href','action'=>'/cert/up2/','title'=>'上传盖章证书'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'type'=>'string','title'=>'姓名'),
			array('name'=>'kechengid','width'=>15,'title'=>'课程','type'=>'select','options'=>'kechengs'),
		)
	);
	$return['kechengs']=pdo_getall('jy_peiedu_kecheng',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['sex']=$this->sex;	
	$return['status']=$this->cert_status;	
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='up2'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['image2']){
			exi('请上传盖章后证书！','error');
		}
		$update=array('status'=>2,'image2'=>$post['image2']);
		pdo_update('jy_peiedu_cert',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_peiedu_cert',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$item=array();
	}
	$item['sex']=$this->sex[$item['sex']];
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'证书',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回证书列表'),
		),
		'edit'=>array(
			array('name'=>'image','title'=>'生成证书','type'=>'img_show'),
			array('name'=>'image2','title'=>'最终证书','type'=>'image'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['sn']){
			exi('请填写证书编号！','error');
		}
		if(!$post['codenumber']){
			exi('请填写身份证！','error');
		}
		$item=pdo_get('jy_peiedu_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['xueshengid']));
		$kecheng=pdo_getcolumn('jy_peiedu_kecheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['kechengid']),'title');
		$poster=$this->plugin_setting('poster');
		$diydata=$poster['data']['data'];
		$url=custom_url('jy_peiedu/cert//xueshengid='.$item['id'],true);
		$qr=cfc('tools')->createqrcode($url,150,true);
		foreach($diydata as &$dv){
			if($dv['type']=='zhaopian'){
				$dv['type']='img';
				$dv['src']=$item['zhaopian'];
			}elseif($dv['type']=='qr'){
				$dv['type']='img';
				$dv['src']=$qr;
			}elseif($dv['type']=='title'){
				$dv['type']='text';
				$dv['value']=$item['title'];
			}elseif($dv['type']=='title'){
				$dv['type']='text';
				$dv['value']=$item['title'];
			}elseif($dv['type']=='sex'){
				$dv['type']='text';
				$dv['value']=$this->sex[$item['sex']];
			}elseif($dv['type']=='codenumber'){
				$dv['type']='text';
				$dv['value']=$item['codenumber'];
			}elseif($dv['type']=='kecheng'){
				$dv['type']='text';
				$dv['value']=$kecheng;
			}elseif($dv['type']=='sn'){
				$dv['type']='text';
				$dv['value']=$post['sn'];
			}
		}
		$poster=array(
			'id'=>'jy_peiedu',
			'height'=>$poster['data']['height']?:990,
			'width'=>$poster['data']['width']?:1360,
			'bg'=>$poster['data']['image']?:'../addons/jy_peiedu/images/cert.jpg',
			'data'=>$diydata,
			'diywidth'=>960
		);
		$update=array('sn'=>$post['sn'],'codenumber'=>$item['codenumber']);
		$update['image']=model('jy_poster/model')->createPoster($poster,'','',$post['xueshengid'],'jy_peiedu');
		pdo_update('jy_peiedu_cert',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_peiedu_cert',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'证书',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回证书列表'),
		),
		'edit'=>array(
			array('name'=>'sn','title'=>'修改证书编号','type'=>'string'),
			array('name'=>'image','title'=>'原生成的证书','type'=>'img_show'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='cert'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_peiedu_cert',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$item=array();
	}
	$item['sex']=$this->sex[$item['sex']];
    $return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'desc'=>'证书',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回证书列表'),
		),
		'look'=>array(
			array('name'=>'image','title'=>'生成的证书效果','type'=>'image'),
			array('name'=>'image2','title'=>'上传的最终证书','type'=>'image'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_peiedu_cert',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_peiedu_cert',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>